
package org.buildforce.tomcat.mojos.management;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.shared.model.fileset.FileSet;
import org.buildforce.tomcat.TomcatManager;
import org.buildforce.tomcat.util.FileSystemUtils;

import java.io.File;

/**
 * Performs a Tomcat cleanup. This includes
 * logs/**, temp/**, work/**.
 *
 * @author  Martin Todorov
 *
 * @goal    clean
 */
public class CleanMojo extends AbstractTomcatMojo
{

    public static final String[] INCLUDES_LOGS = new String[]{ "**/**.log, **/**.out" };
    public static final String[] EXCLUDES_LOGS = new String[]{ "**/**.tar", "**/**.gz*", "**/**.bz2", "**/**.zip" };

    public static final String[] INCLUDES_DEFAULT = new String[]{ "**/**" };

    /**
     * @parameter expression="${clearLogs}" default-value="true" required="false"
     */
    private boolean clearLogs = true;

    /**
     * @parameter expression="${clearTemp}" default-value="true" required="false"
     */
    private boolean clearTemp = true;

    /**
     * @parameter expression="${clearWork}" default-value="true" required="false"
     */
    private boolean clearWork = true;

    /**
     * @parameter expression="${clearWebapps}" default-value="false" required="false"
     */
    private boolean clearWebapps = false;


    public CleanMojo()
    {
    }

    public void execute()
            throws MojoExecutionException, MojoFailureException
    {
        // Make sure Tomcat is stopped first:
        tomcatManager = new TomcatManager(getTomcatConfiguration(), getLog());
        tomcatManager.stopTomcat();

        clearLogs();
        clearTemp();
        clearWork();
        clearWebapps();
    }

    private void clearLogs() throws MojoExecutionException
    {
        if (!clearLogs)
            return;

        String logsDir = getTomcatConfiguration().getTomcatHome() + "/logs";

        FileSet fileSet = FileSystemUtils.createFileSet(logsDir, INCLUDES_LOGS, EXCLUDES_LOGS);

        FileSystemUtils.removeFileSet(fileSet, getLog());

        // noinspection ResultOfMethodCallIgnored
        new File(logsDir).mkdirs();
    }

    private void clearTemp() throws MojoExecutionException
    {
        if (!clearTemp)
            return;

        String tempDir = getTomcatConfiguration().getTomcatHome() + "/temp";

        FileSystemUtils.removeDirectory(new File(tempDir).getAbsoluteFile(), getLog());

        // noinspection ResultOfMethodCallIgnored
        new File(tempDir).mkdirs();
    }

    private void clearWork() throws MojoExecutionException
    {
        if (!clearWork)
            return;

        String workDir = getTomcatConfiguration().getTomcatHome() + "/work";

        FileSystemUtils.removeDirectory(new File(workDir).getAbsoluteFile(), getLog());

        // noinspection ResultOfMethodCallIgnored
        new File(workDir).mkdirs();
    }

    private void clearWebapps() throws MojoExecutionException
    {
        if (!clearWebapps)
            return;

        String webappsDir = getTomcatConfiguration().getTomcatHome() + "/webapps";

        FileSystemUtils.removeDirectory(new File(webappsDir).getAbsoluteFile(), getLog());

        // noinspection ResultOfMethodCallIgnored
        new File(webappsDir).mkdirs();
    }

    public boolean isClearLogs()
    {
        return clearLogs;
    }

    public void setClearLogs(boolean clearLogs)
    {
        this.clearLogs = clearLogs;
    }

    public boolean isClearTemp()
    {
        return clearTemp;
    }

    public void setClearTemp(boolean clearTemp)
    {
        this.clearTemp = clearTemp;
    }

    public boolean isClearWork()
    {
        return clearWork;
    }

    public void setClearWork(boolean clearWork)
    {
        this.clearWork = clearWork;
    }

    public boolean isClearWebapps()
    {
        return clearWebapps;
    }

    public void setClearWebapps(boolean clearWebapps)
    {
        this.clearWebapps = clearWebapps;
    }

}
